import random
# 数据导出
def init_student_data():
    """
    初始化学生数据
    """
    subject = ["Python","Java","C++","数据结构","人工智能"]
    stu=[]
    for i in range(100):
        courseName= subject[i % len(subject)]
        score=random.randint(0, 100)  # 生成0-100的随机整数
        obj = {"no":"A00"+str(i),"name":"学生"+str(i+1),"course":courseName,"score":score}
        stu.append(obj)
    print()
    return stu

def expport_data_to_csv(data,filename="data.xls"):
    """
    将数据导出到CSV文件
    """
    try:
        with open(filename,"w",encoding="utf-8") as f:
            #写入表头
            print("学号，姓名，课程，分数",file=f)

            #写入数据
            for item in data:
                no=item["no"]
                name=item["name"]
                course=item["course"]
                score=item["score"]
                print(f"{no},{name},{course},{score}",file=f)
    except Exception as e:
        debug_print(f"导出数据到CSV文件失败: {e}", level="ERROR")

new_data = init_student_data()
expport_data_to_csv(new_data)